package com.gtw.modules.api.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.gtw.common.exception.RRException;
import com.gtw.common.validator.Assert;
import com.gtw.modules.api.service.TbUserService;
import com.gtw.modules.dao.TbUserDao;
import com.gtw.modules.entity.TbUser;
import com.gtw.utils.UidUtil;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created by wuye on 2017/11/24.
 */
@Service
public class TbUserServiceImpl extends ServiceImpl<TbUserDao, TbUser> implements TbUserService {

    @Autowired
    private TbUserDao userDao;

    @Override
    public TbUser foundById(Long userId) {
        return userDao.selectByPrimaryKey(userId);
    }

    @Override
    public TbUser selectById(Long id) {
        return userDao.selectByPrimaryKey(id);
    }

    @Override
    public TbUser queryByMobile(String mobile) {
        return userDao.queryByMobile(mobile);
    }

    @Override
    public void deleteBatch(Long[] userIds) {
        List<Long> idsList = Arrays.stream(userIds).collect(Collectors.toList());
        userDao.deleteBatchIds(idsList);
    }

    @Override
    public long login(String mobile, String password) {
        TbUser user = queryByMobile(mobile);
        Assert.isNull(user, "手机号或密码错误");
        //密码错误
        if (!user.getPassword().equals(DigestUtils.sha256Hex(password))) {
            throw new RRException("手机号或密码错误");
        }

        return user.getUserId();
    }

    @Override
    public void save(String mobile, String password) {
        TbUser user = new TbUser();
        user.setMobile(mobile);
        user.setUsername(mobile);
        user.setPassword(DigestUtils.sha256Hex(password));
        user.setCreateTime(new Date());
        user.setUuid(UidUtil.getUUID());
        user.setSex(true);
        user.setBalance(0);
        user.setDescription("");
        user.setEnabled(true);
        user.setNickname(mobile);
        user.setProfileimageurl("http://39.108.8.150:8888/group1/M00/00/00/rBK42Vo7I7GAPvRSAABpt_SvtGE918.png");
        userDao.insert(user);
    }

    @Override
    public void save2(TbUser tbUser){
        userDao.updateByPrimaryKey(tbUser);
    }

    @Override
    public int update(TbUser tbuser) {
        return userDao.updateByPrimaryKey(tbuser);
    }

    @Override
    public TbUser queryByUid(String value) {
        return userDao.queryByUid(value);
    }

    @Override
    public Page<TbUser> queryPageList(Page<TbUser> page, Map<String, Object> map) {
        page.setRecords(userDao.queryPageList(page, map));
        return page;
    }

    @Override
    public List<TbUser> queryList(Map<String, Object> map) {
        return userDao.queryList(map);
    }

    /**
     * 获取所有用户列表
     *
     * @return
     */
    @Override
    public List<TbUser> queryAll() {
        return userDao.queryAll();
    }

    @Override
    public TbUser queryOpenId(String openid) {
        return userDao.queryOpenId(openid);
    }

    @Override
    public TbUser queryWBBangd(String wbtoken, String wbopenid) {
        return userDao.queryWBBangd(wbtoken, wbopenid);
    }

    @Override
    public TbUser queryQQBangd(String qqtoken, String qqopenid) {
        return userDao.queryQQBangd(qqtoken, qqopenid);
    }

    @Override
    public Integer add(TbUser record) {
        return userDao.insert(record);
    }

    @Override
    public TbUser queryById(Integer id) {
        return userDao.selectByPrimaryKey(Long.valueOf(id));
    }

    @Override
    public List<TbUser> queryAllBySort() {
        return userDao.queryAllBySort();
    }

    @Override
    public List<TbUser> queryAllBySort1() {
        return userDao.queryAllBySort1();
    }

    @Override
    public int foundByOne(Integer id) {
        return userDao.foundByOne(id);
    }

    @Override
    public List<TbUser> queryByOnd(Map<String, Object> map) {
        return userDao.queryByOnd(map);
    }

    @Override
    public int foundByTwo(Integer id) {
        return userDao.foundByTwo(id);
    }

    @Override
    public List<TbUser> queryByTwo(Map<String, Object> map) {
        return userDao.queryByTwo(map);
    }

    @Override
    public int foundByThree(Integer id) {
        return userDao.foundByThree(id);
    }

    @Override
    public List<TbUser> queryByThree(Map<String, Object> map) {
        return userDao.queryByThree(map);
    }

    @Override
    public int foundByFour(Integer id) {
        return userDao.foundByFour(id);
    }

    @Override
    public List<TbUser> queryByFour(Map<String, Object> map) {
        return userDao.queryByFour(map);
    }

    @Override
    public List<TbUser> queryByintegrallevel1(Integer integrallevel) {
        return userDao.queryByintegrallevel1(integrallevel);
    }

    @Override
    public List<TbUser> queryenergyvaluelevel(Integer energyvaluelevel) {
        return userDao.queryenergyvaluelevel(energyvaluelevel);
    }

    @Override
    public List<TbUser> queryByParentId(Integer userid) {
        return userDao.queryByParentId(userid);
    }
}
